<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="../css/style.css">
    <style>
        form{
            width:270px;
        }
        input{
            width: 64%;
            background: #eee;
        }
        input:focus{
            background: #fff;
        }
        form{
            padding: 0 12px 12px;
        }
        label{
            display: block;
            padding-bottom:12px;
        }
        .label-text{
            width: 36%;
            float: left;
        }
        #upload-tip{
            border: 1px dashed #d9d9d9;
            width: 135px;
            height: 135px;
            line-height: 135px;
            cursor: pointer;
            font-size: 36px;
            color:#d9d9d9;
        }
        #img-show{
            width: 135px;
            height: 135px;
            display: block;
            margin: 0 auto;
            object-fit: cover;
        }
    </style>
    <script>
        function imgfileChange() {
            var upload_tip = document.getElementById("upload-tip");
            var img_show = document.getElementById("img-show");
            var imgfile = document.getElementById("imgfile");

            var freader = new FileReader();
            freader.readAsDataURL(imgfile.files[0]);
            freader.onload = function (e) {
                img_show.src = e.target.result;
                img_show.style.display = "inline";
                upload_tip.style.display = "none";
            };
        }
    </script>
</head>
<body>
   <div id="app">
	    <div id="wrap">
	       <div id="header">
	           <span style="margin-right: 10px;">
	                <script type="text/javascript">
	                  var date = new Date();
	                  document.write("今天是：" + date.getFullYear() + "年" + (date.getMonth() + 1) + "月" + date.getDate() + "日" + " 星期" + "日一二三四五六".charAt(date.getDay()));
	                </script>
	           </span>
	           <h1>旅游信息管理系统</h1>
	       </div>
	       <div id="header-bar"></div>
	       <div id="content" style="height: 480px">
	           <img src="../img/timg.jpg" style="float: right;height: 320px">
	           <h2>添加景点</h2>
	           <form action="viewspotlist.html" method="post">
	               <label>
	                   <div class="label-text">景&emsp;&emsp;点：</div>
	                   <input type="text" name="vname" v-model="place.name">
	               </label>
	               <label>
	                   <div class="label-text">印象图片：</div>
	                   <div style="text-align: center;padding-left: 36%">
	                       <div id="upload-tip">+</div>
	                       <img src="" alt="" id="img-show" style="display: none">
	                       <input type="file" id="imgfile" ref="myFile" style="display: none" onchange="imgfileChange()">
	                   </div>
	               </label>
	               <label>
	                   <div class="label-text">旺季时间：</div>
	                   <input type="text" name="hottime" v-model="place.hottime">
	               </label>
	               <label>
	                   <div class="label-text">旺季门票：</div>
	                   <input type="text" name="hotprice" v-model="place.hotticket">
	               </label>
	               <label>
	                   <div class="label-text">淡季门票：</div>
	                   <input type="text" name="normalprice" v-model="place.dimticket">
	               </label>
				      <label>
				       <div class="label-text">景点描述：</div>
				       <input type="text" name="normalprice" v-model="place.placedes">
				   </label>
	               <label>
	                   <div class="label-text">所属省份：</div>
	                   <select name="ofprovince" v-model="place.provinceid">
	                       <option :value="province.id" v-for="province in provinces" v-text="province.name"></option>
	                   </select>
	               </label>
	               <button type="button" @click="savePlaceInfo">提 交</button>&emsp;
	               <a :href="'viewspotlist.html?provinceid='+id">返回</a>
	           </form>
	       </div>
	       <div id="footer">
	           ABC@126.com
	       </div>
	   </div>
   </div>
</body>
</html>
<script src="../js/vue.js"></script>
<script src="../js/axios.min.js"></script>
<script>
    const app = new Vue({
        el:"#app",
        data:{
         provinces:[],
		 place:{},
		 id:"",
		 admin:''
           
        },
        methods:{
			savePlaceInfo(){
				 //保存景点的方法
				let myFile = this.$refs.myFile;
				let files = myFile.files;
				let file = files[0];
				let formdata = new FormData();
				formdata.append("pic",file);
				formdata.append("name",this.place.name);
				formdata.append("hottime",this.place.hottime);
				formdata.append("hotticket",this.place.hotticket);
				formdata.append("dimticket",this.place.dimticket);
				formdata.append("placedes",this.place.placedes);
				formdata.append("provinceid",this.place.provinceid);
				console.log(formdata);
				axios({
				    method:'post',
				    url:'http://localhost:8080/place/save',
				    data: formdata,
				    headers:{
				        'Content-Type':'multipart/form-data'
				    }
				}).then((res) =>{
				    console.log(res.data);
				    if (res.data.status){
				        alert(res.data.msg+",点击确定回到景点列表");
				        location.href='../province/provincelist.html'
				    }else{
				        alert(res.data.msg);
				    }
				})
			},
			findAllProvinces(){
			    _this = this;
			    axios.get("http://localhost:8080/province/allProvince?page="+1+"&size="+300).then((res) => {
			        _this.provinces = res.data.province;
					//console.log(res.data)
			    })
			},
        },
        created(){
			let id = location.href.substring(location.href.indexOf("=")+1);
			this.id =id
			
						//this.username = localStorage.getItem("username");
			this.admin =JSON.parse(window.localStorage.getItem("access-admin"));
			if(!this.admin){
				window.location='../login.html';
			}else{
				//检验token合法性
				axios({
					url:'http://localhost:8080/user/checkToken',
					method:'get',
					headers:{
						token:this.admin.data.token
					}
				}).then((res) => {
					if(res.data.status){
						//展示数据
						this.findAllProvinces();

					}else{
						//提示信息并返回登录页面
						alert(res.data.msg);
						location.href='../login.html'
					}
				})
			}
			
        }
    })
</script>